package org.taobao.ibatiside.ui.wizard;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.eclipse.emf.common.util.EList;
import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TableViewerColumn;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.wb.swt.SWTResourceManager;
import org.taobao.ibatiside.model.mapperv2.Parameter;
import org.taobao.ibatiside.model.mapperv2.ParameterMap;

public class ShowParameterMapInfoComposite extends Composite {
	private Label idValue;
	private Label classValue;
	private TableViewer tableViewer;
	static final class ParameterWrapper{
		Parameter parameter;
		int index;
		public ParameterWrapper(Parameter parameter, int index) {
			super();
			this.parameter = parameter;
			this.index = index;
		}
	}
	private List<ParameterWrapper> parameters = new ArrayList<ShowParameterMapInfoComposite.ParameterWrapper>();
	public static final class ParameterLabelProvider extends LabelProvider implements ITableLabelProvider{
		@Override
		public Image getColumnImage(Object element, int columnIndex) {
			return null;
		}

		@Override
		public String getColumnText(Object element, int columnIndex) {
			ParameterWrapper wrapper = (ParameterWrapper) element;
			switch (columnIndex){
			case 0:
				return String.valueOf(wrapper.index);
			case 1:
				return (wrapper.parameter.getProperty());
			case 2:
				return wrapper.parameter.getNullValue();
			case 3:
				return wrapper.parameter.getJavaType();
			case 4:
				return wrapper.parameter.getJdbcType();
			case 5:
				return wrapper.parameter.getTypeHandler();
				default:
					return "??";
			}
		}
		
	}

	/**
	 * Create the composite.
	 * @param parent
	 * @param style
	 */
	public ShowParameterMapInfoComposite(Composite parent, int style) {
		super(parent, style);
		setLayout(new GridLayout(1, false));
		
		Composite composite = new Composite(this, SWT.NONE);
		GridData gd_composite = new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1);
		gd_composite.heightHint = 25;
		composite.setLayoutData(gd_composite);
		composite.setLayout(new GridLayout(4, false));
		
		Label lblId = new Label(composite, SWT.NONE);
		lblId.setAlignment(SWT.RIGHT);
		GridData gd_lblId = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
		gd_lblId.widthHint = 50;
		lblId.setLayoutData(gd_lblId);
		lblId.setText("id");
		
		idValue = new Label(composite, SWT.NONE);
		GridData gd_idValue = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
		gd_idValue.widthHint = 150;
		idValue.setLayoutData(gd_idValue);
		idValue.setForeground(SWTResourceManager.getColor(SWT.COLOR_RED));
		idValue.setText("New Label");
		
		Label lblClass = new Label(composite, SWT.NONE);
		lblClass.setAlignment(SWT.RIGHT);
		GridData gd_lblClass = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
		gd_lblClass.widthHint = 50;
		lblClass.setLayoutData(gd_lblClass);
		lblClass.setText("class");
		
		classValue = new Label(composite, SWT.NONE);
		classValue.setForeground(SWTResourceManager.getColor(SWT.COLOR_RED));
		GridData gd_classValue = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
		gd_classValue.widthHint = 200;
		classValue.setLayoutData(gd_classValue);
		classValue.setText("New Label");
		
		tableViewer = new TableViewer(this, SWT.BORDER | SWT.FULL_SELECTION);
		Table table = tableViewer.getTable();
		table.setLinesVisible(true);
		table.setHeaderVisible(true);
		table.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		
		
		TableViewerColumn tableViewerColumn_5 = new TableViewerColumn(tableViewer, SWT.NONE);
		TableColumn tblclmnIndex = tableViewerColumn_5.getColumn();
		tblclmnIndex.setWidth(51);
		tblclmnIndex.setText("Index");
		
		TableViewerColumn tableViewerColumn = new TableViewerColumn(tableViewer, SWT.NONE);
		TableColumn tableColumn = tableViewerColumn.getColumn();
		tableColumn.setWidth(100);
		tableColumn.setText("Property");
		
		TableViewerColumn tableViewerColumn_1 = new TableViewerColumn(tableViewer, SWT.NONE);
		TableColumn tableColumn_1 = tableViewerColumn_1.getColumn();
		tableColumn_1.setWidth(88);
		tableColumn_1.setText("Null Value");
		
		TableViewerColumn tableViewerColumn_2 = new TableViewerColumn(tableViewer, SWT.NONE);
		TableColumn tableColumn_2 = tableViewerColumn_2.getColumn();
		tableColumn_2.setWidth(100);
		tableColumn_2.setText("Java Type");
		
		TableViewerColumn tableViewerColumn_3 = new TableViewerColumn(tableViewer, SWT.NONE);
		TableColumn tableColumn_3 = tableViewerColumn_3.getColumn();
		tableColumn_3.setWidth(91);
		tableColumn_3.setText("JDBC Type");
		
		TableViewerColumn tableViewerColumn_4 = new TableViewerColumn(tableViewer, SWT.NONE);
		TableColumn tableColumn_4 = tableViewerColumn_4.getColumn();
		tableColumn_4.setWidth(209);
		tableColumn_4.setText("Type Handler");
		
		tableViewer.setContentProvider(new ArrayContentProvider());
		tableViewer.setLabelProvider(new ParameterLabelProvider());
		tableViewer.setInput(parameters);
	}

	@Override
	protected void checkSubclass() {
		// Disable the check that prevents subclassing of SWT components
	}

	public void setParameterMap(ParameterMap new_map) {
		idValue.setText(StringUtils.defaultString(new_map.getId()));
		classValue.setText(StringUtils.defaultString(new_map.getClass_()));
		parameters.clear();
		EList<Parameter> ps = new_map.getParameter();
		for (int i = 0; i < ps.size(); i++){
			parameters.add(new ParameterWrapper(ps.get(i), i));
		}
		tableViewer.refresh();
	}
}
